package com.bibrApp.difu.actvity.tibi_mvp;

import android.content.Context;
import android.util.Log;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.bibrApp.difu.Config;
import com.bibrApp.difu.Response.FBInfoResBody;
import com.bibrApp.difu.actvity.record_mvp.RecordBean;
import com.bibrApp.difu.actvity.wallet_mvp.IWalletView;
import com.bibrApp.difu.api.RetrofitNet;
import com.bibrApp.difu.base.BaseObserver;
import com.bibrApp.difu.base.BasePresenter;
import com.bibrApp.difu.utils.DialogUtils;
import com.bibrApp.difu.utils.EncryptUtils;
import com.bibrApp.difu.utils.ToastUtils;
import com.google.gson.Gson;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

public class TibiPresenter extends BasePresenter<ITibiView> {
    public TibiPresenter(Context context, ITibiView iView) {
        super(context, iView);
    }


    public void getAddrData(String usrId, String coinCode) {

        Map<String, String> params = new HashMap<>();
        params.put("userId", usrId);
        params.put("coinCode", coinCode);

        RetrofitNet.getInstance()
                .getApi()
                .getOutAddr(EncryptUtils.encrypt(params))
                .compose(getActivityLifecycleProvider().bindToLifecycle())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(new BaseObserver<Object>(context) {
                    @Override
                    protected void onSuccess(Object value) {
                        //JSONObject jsonObject = JSON.parseObject(EncryptUtils.objectToJson(value));
                        String text = EncryptUtils.objectToJson(value);
                        Log.i("dj", text);
                        List<AddrBean> list = JSON.parseArray(text, AddrBean.class);
                        iView.onAddrResult(list);
                    }

                    @Override
                    protected void onFialure(int code, String msg) {
                        super.onFialure(code, msg);
                        iView.error(msg);
                    }
                });
    }

    public void commit(String getuId, String coinEunit, double num, String addr , String code) {
        Map<String, String> params = new HashMap<>();
        params.put("userId", getuId);
        params.put("coinCode", coinEunit);
        params.put("transactionMoney", String.valueOf(num));
        params.put("outAddress", addr);
        params.put("verifyCode", code);

        RetrofitNet.getInstance()
                .getApi()
                .applyDrawCoin(EncryptUtils.encrypt(params))
                .compose(getActivityLifecycleProvider().bindToLifecycle())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(new BaseObserver<Object>(context) {
                    @Override
                    protected void onSuccess(Object value) {
                        iView.success("提币成功");
                    }

                    @Override
                    protected void onFialure(int code, String msg) {
                        super.onFialure(code, msg);
                        iView.error(msg);
                    }
                });
    }

    /**
     * coinCode	String	是	USDT/BTC/ETH
     * userId	String	是	用户id
     * address	String	是	地址
     * remark	String	是	备注
     */
    public void addAddress(String usrId, String coinCode ,String address ,String remark) {

        Map<String, String> params = new HashMap<>();
        params.put("userId", usrId);
        params.put("coinCode", coinCode);
        params.put("address", address);
        params.put("remark", remark);

        RetrofitNet.getInstance()
                .getApi()
                .addAddress(EncryptUtils.encrypt(params))
                .compose(getActivityLifecycleProvider().bindToLifecycle())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(new BaseObserver<Object>(context) {
                    @Override
                    protected void onSuccess(Object value) {
                        //JSONObject jsonObject = JSON.parseObject(EncryptUtils.objectToJson(value));
                        iView.successOfAddAddress("添加地址成功");
                        String text = EncryptUtils.objectToJson(value);
                        Log.i("dj", text);
                    }

                    @Override
                    protected void onFialure(int code, String msg) {
                        super.onFialure(code, msg);
                        iView.error(msg);
                    }
                });
    }

    /**
     * 获取验证码
     *
     * @param mobile
     */
    public void getCode(String mobile) {
        DialogUtils.showDialog(context);
        HashMap<String, Object> map = new HashMap<>();
        map.put("mobile", mobile);
        map.put("noteType", Config.BASE_URL+Config.GET_APPLY_DRAWCOIN);
        RetrofitNet.getInstance()
                .getApi()
                .getVerifyCode(EncryptUtils.encryptNewType(map))
                .compose(getActivityLifecycleProvider().bindToLifecycle())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .subscribe(new BaseObserver<Object>(context) {

                    @Override
                    protected void onSuccess(Object value) {
                        if(value != null){
                            iView.successOfCode(String.valueOf(value));
                            ToastUtils.ShowToast(context,String.valueOf(value));
                        }

                    }
                });

    }
}
